package edu.mit.media.fv.layers
{
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.icons.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.*;
	public class GroupLayer extends StaticLayer
	{
		private var groups:Array = new Array();
		private var controller:FluidSpace;
		
		public function GroupLayer(controller:FluidSpace, x:int = 0, y:int = 0, width:int = 650, height:int = 100){
			super(x, y, width, height);
			this.backgroundColor = 0x555555;
			this.controller = controller;
			this.reform();
		}
		public override function addBlock(block:Block):void{
			super.addBlock(block);
			if(block == null){ return;}
			block.diameter = 70;
			block.x = Math.max(0, Math.min(block.x, this._viewbounds.width-block.diameter));
			block.y = Math.max(0, Math.min(block.y, this._viewbounds.height-block.diameter));
			
			if(block.model is GroupModel && ! (block in this.groups) ){
				this.groups.push(block);
			}
			trace("Adding block to group layer at: "+block.x+"<"+this.viewbounds.width);
		}
		public override function dropBlock(block:Block):void{
			
			if(block.blockContainer == this){
				trace("moveblock");
				block.moveBlock(this);
				if(block.model is UserModel){
					var group:SpiderBlock = null;
					for( var i:int = 0; i<this.groups.length; i++){
						group = this.groups[i];
						var point:Point = this.localToGlobal(new Point(block.x, block.y));
						if(group.hitTestObject(block)){
							GroupModel(group.model).addModel(block.model);
							block.snapBack();
							
							var data:Object = new Object();
							data.command = FluidConstants.REQUEST_ADD_GROUPUSER;
							data.user_id = block.model.id;
							data.id = group.model.id;
							var event:FVNetworkEvent = new FVNetworkEvent(data);
							FluidEventHandler.dispatchEvent(event);
						}		
					}
				}
			}else if(block.blockContainer is ActiveLayer){
				trace("self destuct");
				block.selfDestruct();
			}else{
				trace("Snap back");
				block.snapBack();
			}
		}
		public override function reform():void{
			this._viewbounds = new Rectangle(
				0, 0,
				this.layerWidth, this.layerHeight);
			
			this._label.x = 0;
			this._label.width = this.layerWidth;
			this._label.height = this.layerHeight;
			this._label.y = 0;
			
			var format:TextFormat = new TextFormat();
			format.align = "center";
			format.bold = true;
			format.font = "Arial";
			format.size = this.layerHeight*0.80;
			format.color = this.backgroundColor;
			this._label.setTextFormat(format);
			
			this._wall.graphics.clear();
			this._wall.graphics.beginFill(0xFFFFFF, 0.01);
			this._wall.graphics.lineStyle(1,0xFFFFFF,0.70,true);
			this._wall.graphics.drawRoundRect(0,0,this.layerWidth, this.layerHeight, 15,15);
			this._wall.graphics.endFill();
		}
		public override function toString():String{
			return "Group Layer";
		}
	}
}